﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using YDModel;
using YDServices.Comm;
using static YDServices.Enum.OrderEnum;

namespace YDServices
{
    public class GildingService : BaseService
    {

        int ordertype = (int)OrderTyp.烫金;
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Order_Details> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    //只查询烫金的数据
              
                    var list = db.T_Order_Details.Where(o=>o.State!=false && o.OrderType== ordertype).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Order_Details>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Order_Details tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        tu.State = true;
                        tu.CreatedTime = GetSqlNow();
                        tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                        db.T_Order_Details.Add(tu);
                        //新增烫金信息之后,减掉染厂的数据
                        var order = db.T_Order_Details.Where(o => o.ID == tu.OrderDetails_ID).FirstOrDefault();
                        if (order != null)
                        {
                            order.UseQuantity = order.UseQuantity + tu.Quantity;
                            order.SurplusQuantity = order.SurplusQuantity - tu.Quantity;

                            order.UseWeight = order.UseWeight + tu.StartWeight;
                            order.SurplusWeight = order.SurplusWeight - tu.StartWeight;
                        } 
                        db.SaveChanges();

                        //同步新增
                        //插入数据到OrderReport
                        var orlList = db.T_Order_Report_Link.Where(o => o.Deying_ID == tu.OrderDetails_ID).ToList();
                        //烫金,拉毛,发货都为空
                        var orl = orlList.Where(o => o.Gilding_ID == null && o.Translate_ID == null
                                && o.Sending_ID == null).FirstOrDefault();
                        if (orl != null) //如果存在 null数据,则更新
                        {
                            orl.Gilding_ID = tu.ID;
                        }
                        else //否则新增
                        {
                            T_Order_Report_Link orlNew = orlList.Where(o => o.Gilding_ID != null || o.Translate_ID != null
                                || o.Sending_ID != null).FirstOrDefault();
                            orlNew.Translate_ID = null;
                            orlNew.Sending_ID = null;
                            orlNew.Gilding_ID = tu.ID;
                            db.T_Order_Report_Link.Add(orlNew);
                        }

                        db.SaveChanges();

                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Order_Details tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Order_Details.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("数据不存在!");
                    }
                    item.Factory_ID = tu.Factory_ID;
                    item.Factory_Name = tu.Factory_Name;
                    item.Spec_ID = tu.Spec_ID;
                    item.Spec_Name = tu.Spec_Name;
                    item.OperatintTime = tu.OperatintTime;

                    item.Quantity = tu.Quantity;
                    item.StartWeight = tu.StartWeight;
                    item.SingleOrDouble = tu.SingleOrDouble;
                    item.ProcessUnitPrice = tu.ProcessUnitPrice;
                    item.ProcessCost = tu.ProcessCost;
                    item.LossPer = tu.LossPer;
                    item.EndWeight = tu.EndWeight;

                    item.ModifiedBy = "["+Global.loginUser.ID+"]"+ Global.loginUser.UserName;
                    item.ModifiedTime = GetSqlNow();  

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Order_Details item)  //List<T_Order_Details> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        var fact = db.T_Order_Details.Where(o => o.ID == item.ID ).FirstOrDefault();
                        if (fact == null)
                        {
                            throw new ApplicationException("数据不存在!");
                        } 
                        fact.State = false;
                        fact.DeletedTime = GetSqlNow();
                        fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                        //删除烫金信息之后,恢复染厂的数据
                        var order = db.T_Order_Details.Where(o => o.ID == fact.OrderDetails_ID).FirstOrDefault();
                        if(order!=null)
                        {
                            order.UseQuantity = order.UseQuantity - fact.Quantity;
                            order.SurplusQuantity = order.SurplusQuantity + fact.Quantity;

                            order.UseWeight = order.UseWeight - fact.StartWeight;
                            order.SurplusWeight = order.SurplusWeight + fact.StartWeight;
                        } 
                        db.SaveChanges();
                        //同步删除OrderReport 中的数据  
                        var orlList = db.T_Order_Report_Link.Where(o => o.Deying_ID == fact.OrderDetails_ID).ToList();
                        var orl = orlList.Where(o => o.Gilding_ID == fact.ID).FirstOrDefault();
                        if (orlList.Count > 1)
                        {
                            db.T_Order_Report_Link.Remove(orl);
                        }
                        else
                        {
                            orl.Gilding_ID = null;
                        }

                        db.SaveChanges();
                        transaction.Commit();

                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }
 

    }
}
